/**
 @Author Zhenglinbin
 @Date 2021/4/28
 **/
import {get} from "@/request/useRequest";

export const replaceAll = (text: string, searchString: string, replacement: string) => {
    return text.replace(new RegExp(searchString, 'g'), replacement);
}
export const unitConvert = async (
    materialCode: string,
    mesUnit: string,
    sapUnit: string,
    orderNumberParam: number | string,
    yesGoodsNumberParam: number | string
    ) => {
    let max = 0
    const unitConvertRes = await get('unitConvert/info',{ materialCode: materialCode,mesUnit,sapUnit })
    if(!unitConvertRes.success || !unitConvertRes.data) {
        throw new Error(`物料编码 ${materialCode} 单位与SAP采购订单不一致，请先配置单位转换`)
    }
    const orderNumber = parseFloat(orderNumberParam as unknown as string)
    const yesGoodsNumber = parseFloat(yesGoodsNumberParam as unknown as string)
    max = (orderNumber * unitConvertRes.data.mesCoefficient / unitConvertRes.data.sapCoefficient) - (yesGoodsNumber * unitConvertRes.data.mesCoefficient / unitConvertRes.data.sapCoefficient)
    return max
}
